package nl.fonfolio.model {
	import nl.fonfolio.core.RemotingService;

	import flash.events.EventDispatcher;
	import flash.net.Responder;
	import flash.events.Event;
	
	import nl.fonfolio.core.FonfolioEvent;	

	/**
	 * @author TEAM PICUS!
	 */
	public class Model extends EventDispatcher {
	
		private var rs:RemotingService;

		//private var project:Object;
		private var portfolio:Object;
		private var profileProject:Object;
		private var project:Object;
		private var level: Object;
		private var currentUser:Object;

		public function Model() {
			rs = new RemotingService("http://fonfolio.nielswijers.nl/amfphp/gateway.php");
		}
		
		public function loadPortfolio(id:Number):void {
			var responder:Responder = new Responder(onLoadPortfolio, onFault);
			rs.call("nl.fonfolio.model.profile.getProfile", responder, id);
		}
		
		private function onLoadPortfolio(result:Object):void {
			this.portfolio = result;
			this.dispatchEvent(new FonfolioEvent("LOAD_PORTFOLIO"));
		}
		
		public function getPortfolio():Object {
			return portfolio;
		}

		private function onFault(result:Object):void {
			trace("Error" + result.description);
		}

		/** ProfileProject **/
		public function loadProfileProject(id:Number):void {
			var responder:Responder = new Responder(onLoadProfileProject, onFault);
			rs.call("nl.fonfolio.model.portfolio.getPortfolio", responder, id);
		}
		
		private function onLoadProfileProject(result:Object):void {
			//returns following fields: .course .description .projectId .title .userId .website .image .rating .countVotes
			this.profileProject = result;
			this.dispatchEvent(new FonfolioEvent("LOAD_PROFILEPROJECT"));
		}
		
		public function getProfileProject():Object {
			return profileProject;
		}
		
		
		/** Project **/
		public function loadProject(projectId:Number):void {
			var responder:Responder = new Responder(onLoadProject, onFault);
			rs.call("nl.fonfolio.model.project.getProject", responder, projectId);
		}
		
		private function onLoadProject(result:Object):void {
			//returns following fields: .course .description .projectId .title .userId .website .image .rating .countVotes
			trace("result requested: " +result.projectId);
			this.project = result;
			this.dispatchEvent(new FonfolioEvent("LOAD_PROJECT"));
		}
		
		public function getProject():Object {
			return this.project;
		}


		/** LEVEL */
		public function loadLevel(startYear:Number):void {
			var responder:Responder = new Responder(onLoadLevel, onFault);
			rs.call("nl.fonfolio.model.level.getLevel", responder, startYear);
		}
		
		private function onLoadLevel(result:Object):void {
			this.level = result;
			trace("level: " + this.level);
			this.dispatchEvent(new FonfolioEvent("LOAD_LEVEL"));
		}
		
		public function getLevel():Object {
			return level;
		}

		/** Login **/

		
		private function onLoginFault(result:Object):void {
			trace("Error" + result.description);
		}
		
		public function doLogin(objLogin:Object):void {
			var responder:Responder = new Responder(onDoLogin, onLoginFault);
			rs.call("nl.fonfolio.model.user.loginUser", responder, objLogin);
		}
		
		private function onDoLogin(result:Object):void {
			trace("MODEL: on do login");
			this.currentUser = result;
			//trace (this.currentUser = result);
			this.dispatchEvent(new FonfolioEvent("LOGIN"));
		}
		
		public function getCurrentUser():Object {
			if (currentUser!=null){
			return currentUser;
			} else return currentUser=null;
		}
		

		
		public function openFullscreen() : void {
			this.dispatchEvent(new FonfolioEvent("OPEN_FULLSCREEN"));
		}
		
		
		/** RATING UPDATE VOTES **/
		public function doInsertVotes(objRating:Object) : void {
			var responder:Responder = new Responder(onDoUpdateVotes, onLoginFault);
			rs.call("nl.fonfolio.model.project.setRating", responder, objRating);
		}
		
		private function onDoUpdateVotes(result:Object) : void {
			this.dispatchEvent(new FonfolioEvent("UPDATE_VOTES"));
		}
	}
}
